﻿
Public Class perftest
    Public Class fceslite
        Private Shared f As freclust.fceslite = Nothing

        Private Shared Function randomkey() As Int64
            Return rnd(0, maxInt16, True)
        End Function

        Private Shared Function randomstring() As String
            Dim rtn As String = Nothing
            For i As Int64 = 0 To 3
                rtn += createGUID()
            Next
            Return rtn
        End Function

        Private Shared Function randombuff() As bytes
            Return New bytes(encoding.utf8().GetBytes(randomstring()))
        End Function

        Private Shared Sub write()
            f.write(randomkey(), randombuff(), Nothing)
        End Sub

        Private Shared Sub append()
            f.append(randomkey(), randombuff(), Nothing)
        End Sub

        Private Shared Sub read()
            f.read(randomkey(), Nothing)
        End Sub

        Private Shared Sub delete()
            f.delete(randomkey())
        End Sub

        Private Shared Sub run()
            Dim round As Int64
            round = 10000
            If isreleasebuild() Then
                round <<= 1
            End If

            randomrun.invoke(round, CType(Nothing, Object), _
                             New randomrun.info(AddressOf write, 0.2), _
                             New randomrun.info(AddressOf append, 0.3), _
                             New randomrun.info(AddressOf read, 0.2), _
                             New randomrun.info(AddressOf delete, 0.2))
        End Sub

        Public Shared Sub go()
            raiseError(errorHandle.errorType.application, "******** start fceslite performance test ********")
            Dim file As String = Nothing
            file = combinepath(filesystem.temppath(), createGUID())
            For i As Int32 = 0 To 3
                f = New freclust.fceslite(file)
                Dim threadCount As Int64
                threadCount = 2
                If isreleasebuild() Then
                    threadCount <<= 1
                End If
                Dim tp As threadpool = Nothing
                tp = New threadpool(threadCount)
                While threadCount > 0
                    tp.insertJob(AddressOf run, Nothing)
                    threadCount -= 1
                End While

                While Not tp.idle()
                    sleep()
                End While
                tp.stop()
                f.close()
            Next
            raiseError(errorHandle.errorType.application, "******** finish fceslite performance test *******")
        End Sub
    End Class
End Class
